/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Blackjack;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;

/**
 *
 * @author Red
 */
public class AreaDibujo extends JPanel {

    private List l;
    private int[][] vEstado;
    private int cEstado, iResalta, jResalta;
    private VentPartida Vent;

    public AreaDibujo() {
        iResalta = jResalta = -1;
        setOpaque(false);
        l = new ArrayList();
        l.add(0);
        l.add(0);
    }

    @Override
    public void paintComponent(Graphics g) {
        pintaFondo(0, 0, g);
        int index = 0;
        int lim = (Integer)l.get(index++);
        for (int i = 0; i < lim; ++i) {
            int oculta = (Integer)l.get(index++);
            if (oculta == 1) {
                pintaCartaOculta(Vent.NCroupier.getX()+63, Vent.NCroupier.getY()-23+i*15, g);
            }
            else {
                int p = (Integer)l.get(index++);
                int n = (Integer)l.get(index++);
                pintaCarta(Vent.NCroupier.getX()+63, Vent.NCroupier.getY()-23+i*15, p, n, g);
            }
        }

        if (iResalta >= 0 && jResalta >= 0) {
            Image resalta = Toolkit.getDefaultToolkit().getImage(getClass().
                            getResource("resalta.png"));
            int resx = Vent.vJugada[iResalta][jResalta].getX()-15;
            int resy = Vent.vJugada[iResalta][jResalta].getY()+5;
            g.drawImage(resalta, resx, resy, resx+200, resy+200, 0, 0, 200, 200, this);
        }

        lim = (Integer)l.get(index++);
        for (int j = 0; j < lim; ++j) {
            int jug = (Integer)l.get(index++);
            for (int ju = 0; ju < jug; ++ju) {
                int cart = (Integer)l.get(index++);
                for (int c = 0; c < cart; ++c) {
                    int oculta = (Integer)l.get(index++);
                    if (oculta == 1) {
                        pintaCartaOculta(Vent.vJugada[j][ju].getX(), Vent.vJugada[j][c].getY()+c*15, g);
                    }
                    else {
                        int p = (Integer)l.get(index++);
                        int n = (Integer)l.get(index++);
                        pintaCarta(Vent.vJugada[j][ju].getX()+3, Vent.vJugada[j][ju].getY()+c*15+26, p, n, g);
                    }
                }
            }
        }
        Image gan = Toolkit.getDefaultToolkit().getImage(getClass().
                    getResource("ganar.png"));
        Image per = Toolkit.getDefaultToolkit().getImage(getClass().
                    getResource("perder.png"));
        Image emp = Toolkit.getDefaultToolkit().getImage(getClass().
                    getResource("empate.png"));
        Image bla = Toolkit.getDefaultToolkit().getImage(getClass().
                    getResource("bkjk.png"));
        for (int i = 0; i < 8; ++i) {
            for (int j = 0; j < 4; ++j) {
                int x = Vent.vJugada[i][j].getX()-24;
                int y = Vent.vJugada[i][j].getY()+17;
                if (vEstado[i][j] == 4) {
                    g.drawImage(bla, x, y, x+200, y+200, 0, 0, 200, 200, this);
                }
                x = Vent.vJugada[i][j].getX()-23;
                y = Vent.vJugada[i][j].getY()+29;
                if (vEstado[i][j] == 1) {
                    g.drawImage(gan, x, y, x+100, y+100, 0, 0, 100, 100, this);
                }
                x = Vent.vJugada[i][j].getX()-28;
                if (vEstado[i][j] == 3) {
                    g.drawImage(emp, x, y, x+100, y+100, 0, 0, 100, 100, this);
                }
                x = Vent.vJugada[i][j].getX()-30;
                if (vEstado[i][j] == 2) {
                    g.drawImage(per, x, y, x+100, y+100, 0, 0, 100, 100, this);
                }
            }
        }
        int x = Vent.NCroupier.getX()+36;
        int y = Vent.NCroupier.getY()-32;
        if (cEstado == 4) {
            g.drawImage(bla, x, y, x+200, y+200, 0, 0, 200, 200, this);
        }
        x = Vent.NCroupier.getX()-23;
        y = Vent.NCroupier.getY()+29;
        if (cEstado == 1) {
            g.drawImage(gan, x, y, x+100, y+100, 0, 0, 100, 100, this);
        }
        x = Vent.NCroupier.getX()-28;
        if (cEstado == 3) {
            g.drawImage(emp, x, y, x+100, y+100, 0, 0, 100, 100, this);
        }
        x = Vent.NCroupier.getX()-30;
        if (cEstado == 2) {
            g.drawImage(per, x, y, x+100, y+100, 0, 0, 100, 100, this);
        }
        super.paintComponent(g);
    }

    public void seleccionar(int i, int j) {
        iResalta = i;
        jResalta = j;
    }

    public void setVent(VentPartida a) {
        Vent = a;
    }

    public void setInformacion(List list, int[][] v, int c) {
        l = list;
        vEstado = v;
        cEstado = c;
    }

    public void pintaFondo(int i, int j, Graphics g) {
        Image b = Toolkit.getDefaultToolkit().
                  getImage(getClass().getResource("fondo.png"));
        g.drawImage(b,
                    i,
                    j,
                    i + 1024,
                    j + 768,
                    0,
                    0,
                    1024,
                    768,
                    this);
    }

    public void pintaCarta(int i, int j, int p, int n, Graphics g) {
        Image b = Toolkit.getDefaultToolkit().getImage(getClass().
                  getResource("Baraja.png"));
        int x = 50*(n-1);
        int y = 0;
        if (p == 4) y = 2*70;
        else if (p == 2) y = 3*70;
        else if (p == 1) y = 1*70;
        else if (p == 3) y = 0*70;
        g.drawImage(b,
                    i,
                    j,
                    i + 50,
                    j + 70,
                    x,
                    y,
                    x + 50,
                    y + 70,
                    this);
    }
    public void pintaCartaOculta(int i, int j, Graphics g) {
        Image b = Toolkit.getDefaultToolkit().getImage(getClass().
                  getResource("Baraja.png"));
        g.drawImage(b,
                    i,
                    j,
                    i + 50,
                    j + 70,
                    150,
                    420,
                    150 + 50,
                    420 + 70,
                    this);
    }
}
